﻿using System;
using EGIConsulting2.WebUI.YahooMap.v1.Util;

namespace EGIConsulting2.WebUI.YahooMap.v1.Geocoding
{
    public class GeoCode
    {
        private static readonly string Request_URL = "http://local.yahooapis.com/MapsService/V1/geocode";

        #region Constructors
        public GeoCode(string appId)
        {
            AppID = appId;
        } 
        #endregion

        #region Properties
        private string _appID;
        public string AppID { get { return _appID; } set { _appID = value; } }

        private string _proxyHost;
        public string ProxyHost { get { return _proxyHost; } set { _proxyHost = value; } }

        private string _proxyUsername;
        public string ProxyUsername { get { return _proxyUsername; } set { _proxyUsername = value; } }

        private string _proxyPassword;
        public string ProxyPassword { get { return _proxyPassword; } set { _proxyPassword = value; } }

        private string _street;
        public string Street { get { return _street; } set { _street = value; } }

        private string _city;
        public string City { get { return _city; } set { _city = value; } }

        private string _state;
        public string State { get { return _state; } set { _state = value; } }

        private int _zip = 0;
        public int Zip { get { return _zip; } set { _zip = value; } }

        private int _zip4 = 0;
        public int Zip4 { get { return _zip4; } set { _zip4 = value; } }

        private string _location;
        public string Location { get { return _location; } set { _location = value; } } 

        private string Request
        {
            get
            {
                string s = Request_URL + "?appid=" + AppID;

                if (!string.IsNullOrEmpty(Location))
                    return String.Format("{0}&location={1}", s, Location); 
                
                if (!string.IsNullOrEmpty(Street))
                    s += String.Format("&street={0}", Street);
                if (!string.IsNullOrEmpty(City))
                    s += String.Format("&city={0}", City);
                if (!string.IsNullOrEmpty(State))
                    s += String.Format("&state={0}", State);
                if (Zip != 0 && Zip4 != 0)
                    s += String.Format("&zip={0}-{1}", Zip, Zip4);
                else if (Zip != 0)
                    s += String.Format("&zip={0}", Zip);
                
                return s;
            }
        }
        #endregion

        #region Methods
        public string GetGeoCode()
        {
            RssReader reader = new RssReader(Request, ProxyHost, ProxyUsername, ProxyPassword);

            //get the raw feed
            return reader.Response();
        } 
        #endregion
    }
}
